import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from warnings import filterwarnings
filterwarnings('ignore')
import statsmodels.api as sm
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
from sklearn.metrics import mean_squared_error, mean_absolute_error
import math
df = pd.read_excel('After Split.xlsx',date_parser=['Date'],index_col='Date')
df.head()
| Close Price | |
|---|---|
| Date | |
| 2019-09-19 | 1101.05 |
| 2019-09-20 | 1199.60 |
| 2019-09-23 | 1257.25 |
| 2019-09-24 | 1253.80 |
| 2019-09-25 | 1239.70 |
df.isna().sum()
Close Price 31 dtype: int64
df.fillna(method='ffill',inplace=True)
df.isna().sum()
Close Price 0 dtype: int64
import plotly.express as pe
pe.box(df['Close Price'],points='all')
df.plot(figsize=(15,8),title='Close Price', fontsize=14)
<AxesSubplot:title={'center':'Close Price'}, xlabel='Date'>
train = df[:451]
test = df[451:]
train['Close Price'].plot(figsize=(15,8),title='Close Price', fontsize=14)
test['Close Price'].plot(figsize=(15,8),title='Close Price', fontsize=14)
plt.show()
dd = np.asarray(train['Close Price'])
y_hat = test.copy()
y_hat['naive'] = dd[len(dd)-1]
plt.figure(figsize=(12,8))
plt.plot(train.index, train['Close Price'], label='Train')
plt.plot(test.index,test['Close Price'], label='Test')
plt.plot(y_hat.index,y_hat['naive'], label='Naive Forecast')
plt.legend(loc='best')
plt.title("Naive Forecast")
plt.show()
mse_naive = mean_squared_error(test, y_hat.naive)
print('MSE: '+str(mse_naive))
mae_naive = mean_absolute_error(test, y_hat.naive)
print('MAE: '+str(mae_naive))
rmse_naive = math.sqrt(mean_squared_error(test, y_hat.naive))
print('RMSE: '+str(rmse_naive))
MSE: 6840.095719178087 MAE: 64.81164383561647 RMSE: 82.70487119377
y_hat_avg = test.copy()
y_hat_avg['avg_forecast'] = test['Close Price'].mean()
plt.figure(figsize=(12,8))
plt.plot(train['Close Price'], label='Train')
plt.plot(test['Close Price'], label='Test')
plt.plot(y_hat_avg['avg_forecast'], label='Average Forecast')
plt.legend(loc='best')
plt.title("Simple Average")
plt.show()
mse_sa = mean_squared_error(test, y_hat.naive)
print('MSE: '+str(mse_sa))
mae_sa = mean_absolute_error(test, y_hat.naive)
print('MAE: '+str(mae_sa))
rmse_sa = math.sqrt(mean_squared_error(test, y_hat.naive))
print('RMSE: '+str(rmse_sa))
MSE: 6840.095719178087 MAE: 64.81164383561647 RMSE: 82.70487119377
y_hat_avg = test.copy()
y_hat_avg['moving_avg_forecast'] = test['Close Price'].rolling(30).mean().iloc[-1]
plt.figure(figsize=(16,8))
plt.plot(train['Close Price'], label='Train')
plt.plot(test['Close Price'], label='Test')
plt.plot(y_hat_avg['moving_avg_forecast'], label='Moving Average Forecast')
plt.legend(loc='best')
plt.title("Moving Average Forecast")
plt.show()
mse_ma = mean_squared_error(test, y_hat_avg['moving_avg_forecast'])
print('MSE: '+str(mse_ma))
mae_ma = mean_absolute_error(test, y_hat_avg['moving_avg_forecast'])
print('MAE: '+str(mae_ma))
rmse_ma = math.sqrt(mean_squared_error(test, y_hat_avg['moving_avg_forecast']))
print('RMSE: '+str(rmse_ma))
MSE: 5903.774878234405 MAE: 59.33744292237448 RMSE: 76.83602591385375
y_hat_avg = test.copy()
fit2 = SimpleExpSmoothing(np.asarray(train['Close Price'])).fit(smoothing_level=0.7,optimized=False)
y_hat_avg['SES'] = fit2.forecast(len(test))
plt.figure(figsize=(16,8))
plt.plot(train['Close Price'], label='Train')
plt.plot(test['Close Price'], label='Test')
plt.plot(y_hat_avg['SES'], label='SES')
plt.legend(loc='best')
plt.title("Simple Exponential Smoothing")
plt.savefig('SES.png')
mse_ses = mean_squared_error(test, y_hat_avg['SES'])
print('MSE: '+str(mse_ses))
mae_ses = mean_absolute_error(test,y_hat_avg['SES'])
print('MAE: '+str(mae_ses))
rmse_ses = math.sqrt(mean_squared_error(test, y_hat_avg['SES']))
print('RMSE: '+str(rmse_ses))
MSE: 6785.286521168236 MAE: 64.4897662050722 RMSE: 82.37285063155358
sm.tsa.seasonal_decompose(train['Close Price'],period=30,model='additive').plot()
result = sm.tsa.stattools.adfuller(train['Close Price'])
plt.show()
result
(-0.8687373431296384,
0.7980932839243056,
6,
444,
{'1%': -3.4451642100030084,
'5%': -2.8680716196949327,
'10%': -2.5702491139112085},
3989.7820701732276)
y_hat_avg = test.copy()
fit1 = Holt(np.asarray(train['Close Price'])).fit(smoothing_level = 0.3,smoothing_slope = 0.1)
y_hat_avg['Holt_linear'] = fit1.forecast(len(test))
plt.figure(figsize=(16,8))
plt.plot(train['Close Price'], label='Train')
plt.plot(test['Close Price'], label='Test')
plt.plot(y_hat_avg['Holt_linear'], label='Holt_linear')
plt.legend(loc='best')
plt.title("Holt's Linear Trend Method")
plt.savefig("HoltsLinear Method.png")
mse_hl = mean_squared_error(test, y_hat_avg['Holt_linear'])
print('MSE: '+str(mse_hl))
mae_hl = mean_absolute_error(test,y_hat_avg['Holt_linear'])
print('MAE: '+str(mae_hl))
rmse_hl = math.sqrt(mean_squared_error(test, y_hat_avg['Holt_linear']))
print('RMSE: '+str(rmse_hl))
MSE: 9027.790808752985 MAE: 68.75672141328316 RMSE: 95.01468733176458
y_hat_avg = test.copy()
fit1 = ExponentialSmoothing(np.asarray(train['Close Price']) ,seasonal_periods=30 ,trend='add', seasonal='add',).fit()
y_hat_avg['Holt_Winter'] = fit1.forecast(len(test))
plt.figure(figsize=(16,8))
plt.plot( train['Close Price'], label='Train')
plt.plot(test['Close Price'], label='Test')
plt.plot(y_hat_avg['Holt_Winter'], label='Holt_Winter')
plt.legend(loc='best')
plt.title("Holt-Winters Method")
plt.savefig('Holt-Winter.png')
mse_hw = mean_squared_error(test, y_hat_avg['Holt_Winter'])
print('MSE: '+str(mse_hw))
mae_hw= mean_absolute_error(test,y_hat_avg['Holt_Winter'])
print('MAE: '+str(mae_hw))
rmse_hw = math.sqrt(mean_squared_error(test, y_hat_avg['Holt_Winter']))
print('RMSE: '+str(rmse_hw))
MSE: 4386.615024881044 MAE: 53.02881742601117 RMSE: 66.23152591388065
cols = ['Model_Name', 'MSE', 'RMSE', 'MAE']
Score_Card = pd.DataFrame(columns = cols)
naive = pd.Series({'Model_Name': "Naive Approach",
'MSE': mse_naive,
'RMSE': rmse_naive,
'MAE':mae_naive
})
Score_Card = Score_Card.append(naive,ignore_index=True)
sa = pd.Series({'Model_Name': "Simple Average",
'MSE': mse_sa,
'RMSE': rmse_sa,
'MAE':mae_sa
})
Score_Card = Score_Card.append(sa,ignore_index=True)
ma = pd.Series({'Model_Name': "Moving Average",
'MSE': mse_ma,
'RMSE': rmse_ma,
'MAE':mae_ma
})
Score_Card = Score_Card.append(ma,ignore_index=True)
ses = pd.Series({'Model_Name': "Simple Exponential Smoothing",
'MSE': mse_ses,
'RMSE': rmse_ses,
'MAE':mae_ses
})
Score_Card = Score_Card.append(ses,ignore_index=True)
hl = pd.Series({'Model_Name': "Holt’s Linear Trend method",
'MSE': mse_hl,
'RMSE': rmse_hl,
'MAE':mae_hl
})
Score_Card = Score_Card.append(hl,ignore_index=True)
hw = pd.Series({'Model_Name': "Holt’s Winter method",
'MSE': mse_hw,
'RMSE': rmse_hw,
'MAE':mae_hw
})
Score_Card = Score_Card.append(hw,ignore_index=True)
Score_Card.set_index('Model_Name',inplace=True)
Score_Card
| MSE | RMSE | MAE | |
|---|---|---|---|
| Model_Name | |||
| Naive Approach | 6840.095719 | 82.704871 | 64.811644 |
| Simple Average | 6840.095719 | 82.704871 | 64.811644 |
| Moving Average | 5903.774878 | 76.836026 | 59.337443 |
| Simple Exponential Smoothing | 6785.286521 | 82.372851 | 64.489766 |
| Holt’s Linear Trend method | 9027.790809 | 95.014687 | 68.756721 |
| Holt’s Winter method | 4386.615025 | 66.231526 | 53.028817 |